﻿using ChartingModel.Collections.Series;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using ChartingModel.Style;
using ChartingModel.Collections;

namespace WpfChartingControlLibrary.BarCharts
{
    /// <summary>
    /// Interaction logic for BarChartControl1.xaml
    /// </summary>
    public partial class BarChartControl11 : UserControl
    {

        public static DependencyProperty TitleProperty = DependencyProperty.Register("Title", typeof(string), typeof(BarChartControl1), new PropertyMetadata(new PropertyChangedCallback(ShowValueLabelsChanged)));

        public static DependencyProperty XAxisTitleProperty = DependencyProperty.Register("XAxisTitle", typeof(string), typeof(BarChartControl1), new PropertyMetadata(new PropertyChangedCallback(ShowValueLabelsChanged)));

        public static DependencyProperty YAxisTitleProperty = DependencyProperty.Register("YAxisTitle", typeof(string), typeof(BarChartControl1), new PropertyMetadata(new PropertyChangedCallback(ShowValueLabelsChanged)));



        public static DependencyProperty DataSeriesBarProperty = DependencyProperty.Register("DataSeriesBar", typeof(DataSeriesBar), typeof(BarChartControl1));

        public static DependencyProperty DataCollectionProperty = DependencyProperty.Register("DataCollection", typeof(BarChartDataList), typeof(BarChartControl1));

        public static DependencyProperty ValueLabelsShowProperty = DependencyProperty.Register("ShowValueLabels", typeof(Visibility), typeof(BarChartControl1), new PropertyMetadata(new PropertyChangedCallback(ShowValueLabelsChanged)));

        public static DependencyProperty ValueLabelsMinFontSizeProperty = DependencyProperty.Register("ValueLabelsMinFontSize", typeof(double), typeof(BarChartControl1), new PropertyMetadata(new PropertyChangedCallback(ShowValueLabelsChanged)));

        public static DependencyProperty ShowValueToolTipProperty = DependencyProperty.Register("ShowValueToolTip", typeof(bool), typeof(BarChartControl1), new PropertyMetadata(new PropertyChangedCallback(ShowValueLabelsChanged)));

        public static DependencyProperty ChartStyleProperty = DependencyProperty.Register("ChartStyle", typeof(ChartStyleGridLines), typeof(BarChartControl1)); 

        public BarChartControl11()
        {
            DefaultStyleKeyProperty.OverrideMetadata(typeof(BarChartControl1), new FrameworkPropertyMetadata(typeof(BarChartControl1)));
     //       InitializeComponent();
            this.DataCollection = new BarChartDataList();
            this.DataSeriesBar = new DataSeriesBar();
            this.ChartStyle = new ChartStyleGridLines();
            this.ChartStyle.ChartCanvas = chartCanvas;
            this.ChartStyle.TextCanvas = textCanvas;
        }

        public string Title
        {
            get { return (string)GetValue(TitleProperty); }
            set { SetValue(TitleProperty, value); }
        }

        public string XAxisTitle
        {
            get { return (string)GetValue(XAxisTitleProperty); }
            set { SetValue(XAxisTitleProperty, value); }
        }

        public string YAxisTitle
        {
            get { return (string)GetValue(YAxisTitleProperty); }
            set { SetValue(YAxisTitleProperty, value); }
        }

        public BarChartDataList DataCollection
        {
            get { return (BarChartDataList)GetValue(DataCollectionProperty); }
            set { SetValue(DataCollectionProperty, value); }
        }

        public DataSeriesBar DataSeriesBar
        {
            get { return (DataSeriesBar)GetValue(DataSeriesBarProperty); }
            set { SetValue(DataSeriesBarProperty, value); }
        }

        public ChartStyleGridLines ChartStyle
        {
            get { return (ChartStyleGridLines)GetValue(ChartStyleProperty); }
            set { SetValue(ChartStyleProperty, value); }
        }

        public Visibility ShowValueLabels
        {
            get { return (Visibility)GetValue(ValueLabelsShowProperty); }
            set { SetValue(ValueLabelsShowProperty, value); }
        }

        public double ValueLabelsMinFontSize
        {
            get { return (double)GetValue(ValueLabelsMinFontSizeProperty); }
            set { SetValue(ValueLabelsMinFontSizeProperty, value); }
        }

        public bool ShowValueToolTip
        {
            get { return (bool)GetValue(ShowValueToolTipProperty); }
            set { SetValue(ShowValueToolTipProperty, value); }
        }


        private static void ShowValueLabelsChanged(DependencyObject sender, DependencyPropertyChangedEventArgs e)
        {

            BarChartControl1 bcc = (BarChartControl1)sender;
            bcc.chartCanvas.Children.RemoveRange(1, bcc.chartCanvas.Children.Count - 1);
            bcc.textCanvas.Children.RemoveRange(1, bcc.textCanvas.Children.Count - 1);
            if (e.Property == ValueLabelsShowProperty)
                bcc.DataCollection.LabelValuesVisibility = (Visibility)e.NewValue;
            if (e.Property == ShowValueToolTipProperty)
                bcc.DataCollection.ActiveToolTip = (bool)e.NewValue;
            if (e.Property == ValueLabelsMinFontSizeProperty)
                bcc.DataCollection.LabelMinimumFontSize = (double)e.NewValue;
            if (e.Property == TitleProperty)
                bcc.ChartStyle.Title = (string)e.NewValue;
            if (e.Property == XAxisTitleProperty)
                bcc.ChartStyle.XLabel = (string)e.NewValue;
            if (e.Property == YAxisTitleProperty)
                bcc.ChartStyle.YLabel = (string)e.NewValue;

            bcc.AddChart();
        }
      
        private void chartGrid_SizeChanged(object sender, SizeChangedEventArgs e)
        {
            this.DataSeriesBar = new ChartingModel.Collections.Series.DataSeriesBar();
            textCanvas.Width = chartGrid.ActualWidth;
            textCanvas.Height = chartGrid.ActualHeight;
            legendCanvas.Children.Clear();
            chartCanvas.Children.RemoveRange(1, chartCanvas.Children.Count - 1);
            textCanvas.Children.RemoveRange(1, textCanvas.Children.Count - 1);
            AddChart();
            
        }

        private void AddChart()
        {
            this.ChartStyle.AddChartStyle(PART_TitleTextBlock, PART_XLabelTextBlock, PART_YLabelTextBlock, true, true);
            if (this.DataCollection.DataList.Count != 0)
            {
                this.DataCollection.AddBars(this.ChartStyle);
            }
        }

        public override void OnApplyTemplate()
        {
            base.OnApplyTemplate();
            TextBlock S = GetTemplateChild("PART_TitleTextBlock") as TextBlock;
        }

    }
}
